home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / SunSoft.CafeDelSol / ScrollMap / ScrollMap.test < prev    next >
Text File  |  1996-02-16  |  15KB  |  496 lines

  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import java.net.*;
  4. import java.net.MalformedURLException;
  5. import java.util.StringTokenizer;
  6.  
  7. class ImageLoader extends Thread {
  8.         ScrollMap parent;
  9.         Image img;
  10.         
  11.         ImageLoader (ScrollMap parent) {
  12.                 this.parent = parent;
  13.                 start();
  14.                 }
  15.         public void run() {
  16.         int n = 0;
  17.                 while (n < (parent.nimgs -1)) {
  18.         n++;
  19.         img = parent.getImage (parent.getDocumentBase(), parent.img_dir + "/" + parent.urlarr[n]);
  20.  
  21.  
  22.  
  23.                   if (parent.numimgarr[n] != 0) {
  24.           parent.debug(" numimgarr " + parent.numimgarr[n] + " urlarr " + parent.urlarr[n]);
  25.             parent.animgarr[n] = new MyImage( parent, n, parent.numimgarr[n], parent.urlarr[n]);
  26.           }
  27.  
  28.  
  29.  
  30.         parent.ldimage =  false;
  31.                 parent.add(img, n);
  32.            }
  33.         }
  34. }
  35.  
  36. class MyImage extends Thread {
  37.  
  38.     Image animg[] ;
  39.     ScrollMap parent;
  40.     String url;
  41.  
  42.     int N;
  43.     int which = 1;
  44.     int skip = 0;
  45.  
  46. public void run() {
  47.      for ( int nn=1;nn < (parent.numimgarr[N] + 1);nn++) {
  48.          animg[nn] = parent.getImage (parent.getDocumentBase(), parent.img_dir + "/" + parent.urlarr[N] + nn);
  49.         //System.out.println(parent.numimgarr[N] + " " + parent.urlarr[N] + nn);
  50.      }
  51. }
  52.  
  53. MyImage ( ScrollMap parent, int n, int nimgs, String url) {
  54.  
  55. this.parent = parent;
  56. N = n;
  57. this.url = url;
  58. animg = new Image[nimgs+1];
  59.  
  60. start();
  61. }
  62.  
  63. public class ScrollMap extends java.applet.Applet implements Runnable {
  64.  
  65.     Image offscrImg;
  66.     Graphics offscrG; 
  67.  
  68.     boolean onarr[];
  69.  
  70.     Image imgs[];
  71.     MyImage animgarr[];
  72.  
  73.     String param; 
  74.     String prev; 
  75.     String current; 
  76.  
  77.     String urlarr[] = new String [64];
  78.     String destarr[] = new String [64];
  79.     String namearr[] = new String [64];
  80.     String soundarr[] = new String [64];
  81.  
  82.     int numimgarr[] = new int [64];
  83.     int skiparr[] = new int [64];
  84.     int backarr[] = new int [64];
  85.     int widtharr[] = new int [64];
  86.     int accum[] = new int [64];
  87.     int num = 1;
  88.     int n;
  89.     int pos;
  90.     int fix;
  91.     int newpos;
  92.     int nimgs;
  93.     int width;
  94.     int height;
  95.     int totalwidth = 0;  
  96.     int x, newx;
  97.     int dist = 1;
  98.     int timeout = 200;
  99.     int mousepos;
  100.  
  101.     Thread scroller;
  102.  
  103.     String img_dir;
  104.     String showstat;
  105.     String before;
  106.     String after;
  107.     String url;
  108.     String dir;
  109.     String statuschk;
  110.  
  111.     boolean ldimage = false;
  112.     boolean loaded;
  113.     boolean top;
  114.     boolean in = false;
  115.  
  116.     URL anchor;
  117.  
  118.  
  119.     public void debug (String s) {
  120.     System.out.println(s);
  121.     }
  122.    
  123.     public void init () {
  124.  
  125.     String at = getParameter("before");
  126.     before = (at != null) ? at : "";
  127.     at = getParameter("after");
  128.     after = (at != null) ? at : "";
  129.     at = getParameter("img_dir");
  130.     img_dir = (at != null) ? at : "doc:/demo/images/duke";
  131.     at = getParameter("speed");
  132.     timeout = 1000 / ((at == null) ? 4 : Integer.valueOf(at).intValue());
  133.     at = getParameter("dir");
  134.     dist = (at == null) ? 5 : Integer.valueOf(at).intValue();
  135.         width = size().width;
  136.         height = size().height;
  137.  
  138.     newx = x = size().width;
  139.  
  140.         while (true) {   
  141.                 nimgs++;
  142.  
  143.         urlarr[nimgs] =  getParameter("url"+nimgs);
  144.         destarr[nimgs] = getParameter("dest"+nimgs);
  145.         namearr[nimgs] = getParameter("name"+nimgs);
  146.         soundarr[nimgs] = getParameter("sound"+nimgs);
  147.                 if (getParameter("background"+nimgs) != null) {
  148.                 backarr[nimgs] = Integer.valueOf(getParameter("background"+nimgs)).intValue();
  149.                 } else {
  150.                 backarr[nimgs] = 0;
  151.                 }
  152.             if (getParameter("skip"+nimgs) != null) {
  153.         skiparr[nimgs] = Integer.valueOf(getParameter("skip"+nimgs)).intValue(); 
  154.         } else {
  155.         skiparr[nimgs] = 0;
  156.         }
  157.             if (getParameter("numimg"+nimgs) != null) {
  158.         numimgarr[nimgs] = Integer.valueOf(getParameter("numimg"+nimgs)).intValue();
  159.         } else {
  160.         numimgarr[nimgs] = 0;
  161.         }
  162.                 if (urlarr[nimgs] == null) {
  163.                 break;
  164.                 }
  165.         }        
  166.     onarr = new boolean[nimgs+1];
  167.     imgs = new Image[nimgs+1];
  168.         animgarr = new MyImage[nimgs];
  169.  
  170.                 offscrImg = createImage(width, height);
  171.                 offscrG = offscrImg.getGraphics();
  172.  
  173.     }
  174.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
  175.         if ((flags & WIDTH) != 0 ) {
  176.       n++;
  177.       if (n >= (nimgs-1) ){
  178.         for (n=1;n < nimgs; n++) {
  179.             int tmpW = imgs[n].getWidth(this);
  180.             totalwidth +=  tmpW;
  181.             widtharr[n] = tmpW;
  182.         }
  183.       }
  184.         }
  185.         return super.imageUpdate(img, flags, x, y, w, h);
  186.     }
  187.     public void showDocument(URL u) {
  188.         getAppletContext().showDocument(u);
  189.     }
  190.  
  191.     public synchronized void add(Image img, int n) {
  192.         imgs[n] = img;
  193.         
  194.         if (isActive()) {
  195.             repaint();
  196.         }
  197.         }
  198.  
  199.     public void start() {
  200.         if (!loaded) {
  201.             loaded = true;
  202.             new ImageLoader(this);
  203.         }
  204.     if (scroller == null) {
  205.         scroller = new Thread(this);
  206.         scroller.start();
  207.     } else {
  208.         scroller.resume();
  209.     }
  210.     }
  211.     public void stop() {
  212.     if (scroller != null ) {
  213.         scroller.suspend();
  214.     }
  215.     }
  216.  
  217.     public void run() {
  218.     while (true) {
  219.     try {Thread.currentThread().sleep(timeout);} catch (InterruptedException e){}
  220.         scroll(dist);
  221.     }
  222.     }
  223.  
  224.     synchronized void scroll(int dist) {
  225.     repaint();
  226.     }
  227.  
  228.     public void update(Graphics g) {
  229.         newx += dist;
  230.     g.setColor(Color.lightGray);
  231.  
  232.     if (newx != x) {
  233.         int dist = newx - x;
  234.         if (dist > 0) {
  235.         for (x = newx ; x > width ; x -= Math.max(width - 2, totalwidth));
  236.         paint(g, 1, dist + 1);
  237.         } else {
  238.         for (x = newx ; x < 0 ; x += Math.max(width - 2, totalwidth));
  239.         paint(g, (width-1) + dist, width-1);
  240.         }
  241.         if ( in ) {
  242.         statusShow();
  243.         }
  244.     } else {
  245.         //paint(g);
  246.     }
  247.     }
  248.     public void paint(Graphics g, int fromx, int tox) {
  249.     paintbuffer(offscrG, fromx, tox);
  250.     g.drawImage(offscrImg, 0, 0, this);
  251.     }
  252.         
  253.  
  254.     public void paintbuffer(Graphics g, int fromx, int tox) {
  255.     int x = this.x;
  256.     newx = x;
  257.         fix = x;
  258.     int h = 0;
  259.     int w = 0;
  260.         int accum =0;
  261.         int tmpwidth =0;
  262.  
  263.     g.setColor(Color.lightGray);
  264.     g.fillRect(1, 1, width, height-2);
  265.     g.clipRect(1, 1, width, height-2);
  266.  
  267.  
  268.     for (int i = 0 ; i < nimgs ; i++) {
  269.         if (imgs[i] == null) {
  270.         continue;
  271.         }
  272.         w = imgs[i].getWidth(this);
  273.         h = imgs[i].getHeight(this);
  274.  
  275.  
  276.               if ( numimgarr[i] != 0 ) {
  277.                 if (backarr[i] != 0) {
  278.                 g.drawImage(imgs[i], x, height - (h+1), this);
  279.                 }
  280.                 if (animgarr[i].skip == skiparr[i] ) {
  281.                 animgarr[i].skip = 0;
  282.                 g.drawImage(animgarr[i].animg[animgarr[i].which], (x - tmpwidth), height - (h+1), this);     
  283.                 animgarr[i].which++;
  284.                 } else {    
  285.                 g.drawImage(animgarr[i].animg[animgarr[i].which], (x - tmpwidth), height - (h+1), this);     
  286.                 animgarr[i].skip++;
  287.                 }
  288.                 if (animgarr[i].which > numimgarr[i]) {
  289.                 animgarr[i].which = 1;
  290.                 }
  291.  
  292.               } else {
  293.                 g.drawImage(imgs[i], x, height - (h+1), this);
  294.               }  
  295.  
  296.             if ((x + w) > width) {
  297.                 x -= Math.max(width - 2, totalwidth);
  298.             }
  299.                 x += w;
  300.         }
  301. /*
  302. System.out.println( x + " ");
  303.                   for (int t = 1;t<nimgs+1;t++) {
  304.           tmpwidth = widtharr[t];
  305.           accum += tmpwidth; 
  306.  
  307.                    if (numimgarr[t] != 0) {
  308.                      g.drawImage(animgarr[t].animg[animgarr[t].which], (x - tmpwidth), height - (h+1), this);          
  309.                     animgarr[t].which++;
  310.                     if (animgarr[t].which > numimgarr[t]) {
  311.                         animgarr[t].which = 1;
  312.                     }
  313.                     }
  314.                    }
  315.  
  316. */
  317.         
  318.     }
  319.  
  320.  
  321.     public void paint(Graphics g) {
  322.  
  323.     //offscrG.draw3DRect(0, 0, width, height, true);
  324.     paint(g, 1, width-1);
  325.     }
  326.  
  327.    public void statusShow (){
  328.  
  329.        showStatus( showstat ) ;
  330.        accum[0] = 0;
  331.        pos = (-fix);
  332.        newpos = ((totalwidth - fix) - (totalwidth - width));
  333.        for (int i = 0 ;i < (nimgs - 1) ; i++) {
  334.  
  335.          if (pos < 0 ) {
  336.                 if ((width -mousepos) < newpos) {
  337.                     accum[0] = (accum[0] + widtharr[i]);
  338.                     if ( (newpos - (width - mousepos)) >= (accum[0]) && (newpos - (width - mousepos ) ) <= (accum[0]+widtharr[i+1])){
  339.             current = urlarr[i+1];
  340.             if (!current.equals(prev)) { onarr[i+1] = false;} else { onarr[i+1] = true;} 
  341.             if (soundarr[i+1] != null && !onarr[i+1]) { play(getCodeBase(), soundarr[i+1]);}
  342.  
  343.             if (destarr[i+1] != null ) {
  344.             if (namearr[i+1] !=null ) {
  345.                                 showstat = "Click to select: " + namearr[i+1];
  346.             } else {
  347.                             if ( destarr[i+1].equals("same")) { 
  348.                                 showstat = "Click to select: " + urlarr[i+1];
  349.                             } else {   
  350.                                 showstat = "Click to select: " + destarr[i+1];
  351.                             } 
  352.             }
  353.                 } else {
  354.                showstat = " ";        
  355.                     }            
  356.                     break;
  357.                    }
  358.                 } else {
  359.                     accum[0] = (accum[0] + widtharr[i]);
  360.                     if ( ( (totalwidth - fix) + mousepos  >= accum[0] ) &&  ( (totalwidth - fix) + mousepos  <= (accum[0] + widtharr[i+1]) ) ) {
  361.             current = urlarr[i+1];
  362.             if (!current.equals(prev)) { onarr[i+1] = false;} else { onarr[i+1] = true;} 
  363.             if (soundarr[i+1] != null && !onarr[i+1]) { play(getCodeBase(), soundarr[i+1]);}
  364.                     if (destarr[i+1] != null) { 
  365.                         if (namearr[i+1] != null ) { 
  366.                                 showstat = "Click to select: " + namearr[i+1];
  367.                         } else { 
  368.                     if ( destarr[i+1].equals("same")) {
  369.                                 showstat = "Click to select: " + urlarr[i+1];
  370.                 } else {
  371.                                 showstat = "Click to select: " + destarr[i+1];
  372.                             }                     
  373.                         } 
  374.                     } else {             
  375.                        showstat = " ";  
  376.                     } 
  377.                     break;
  378.                 }
  379.          }
  380.        } else {
  381.                    accum[0] = (accum[0] + widtharr[i]);
  382.                    if ( (pos + mousepos) >= (accum[0]) && (pos + mousepos ) <= (accum[0]+widtharr[i+1])){
  383.             current = urlarr[i+1];
  384.             if (!current.equals(prev)) { onarr[i+1] = false;} else { onarr[i+1] = true;} 
  385.             if (soundarr[i+1] != null && !onarr[i+1]) { play(getCodeBase(), soundarr[i+1]);}
  386.                     if (destarr[i+1] != null) { 
  387.                         if (namearr[i+1] != null ) { 
  388.                                 showstat = "Click to select: " + namearr[i+1];
  389.                         } else { 
  390.                             if ( destarr[i+1].equals("same") ) { 
  391.                                 showstat = "Click to select: " + urlarr[i+1];
  392.                             } else {   
  393.                                 showstat = "Click to select: " + destarr[i+1];
  394.                             } 
  395.                         } 
  396.                     } else {             
  397.                        showstat = " ";  
  398.                     } 
  399.                         break;
  400.                     }
  401.                 }
  402.         }
  403.  
  404.     prev = current;
  405.         return;
  406.     }
  407.  
  408.     public boolean handleEvent(Event evt) {
  409.     boolean mouse = true;
  410.     if (evt.id == Event.MOUSE_DOWN) {
  411.         
  412.        int mdx = evt.x;
  413.  
  414.        accum[0] = 0;
  415.        pos = (-fix);
  416.        newpos = ((totalwidth - fix) - (totalwidth - width));
  417.        for (int i = 0 ;i < (nimgs -1) ; i++) {
  418.  
  419.          if (pos < 0 ) {
  420.                 if ((width -mdx) < newpos) {
  421.                     accum[0] = (accum[0] + widtharr[i]);
  422.                     if ( (newpos - (width - mdx)) >= (accum[0]) && (newpos - (width - mdx ) ) <= (accum[0]+widtharr[i+1])){
  423.             if ( destarr[i+1] == null ) { continue; }
  424.             if ( destarr[i+1].equals("same") ) { 
  425.                        try {
  426.                        anchor = new URL(null,  before + urlarr[i+1] + after);
  427.                        } catch (MalformedURLException e) {
  428.                        anchor = null;
  429.                        }
  430.                     } else {
  431.                        try { 
  432.                        anchor = new URL(null,  destarr[i+1]);showStatus("Fetching ...");showDocument(anchor);
  433.                        } catch (MalformedURLException e) { 
  434.                        anchor = null; 
  435.                        }
  436.                     }
  437.                    break;
  438.                    }
  439.                 } else {
  440.                     accum[0] = (accum[0] + widtharr[i]);
  441.                     if ( ( (totalwidth - fix) + mdx  >= accum[0] ) &&  ( (totalwidth - fix) + mdx  <= (accum[0] + widtharr[i+1]) ) ) {
  442.                     if ( destarr[i+1] == null ) { continue; }
  443.                     if ( destarr[i+1].equals("same") ) { 
  444.                        try {  
  445.                        anchor = new URL(null,  before + urlarr[i+1] + after);showStatus("Fetching ...");showDocument(anchor); 
  446.                        } catch (MalformedURLException e) {  
  447.                        anchor = null; 
  448.                        } 
  449.                     } else {
  450.                        try {   
  451.                        anchor = new URL(null,  destarr[i+1]);showStatus("Fetching ...");showDocument(anchor); 
  452.                        } catch (MalformedURLException e) {   
  453.                        anchor = null;  
  454.                        } 
  455.                     }
  456.                     break;
  457.                 }
  458.          }
  459.        } else {
  460.                    accum[0] = (accum[0] + widtharr[i]);
  461.                    if ( (pos + mdx) >= (accum[0]) && (pos + mdx ) <= (accum[0]+widtharr[i+1])){
  462.                     if ( destarr[i+1] == null ) { continue; }
  463.                     if ( destarr[i+1].equals("same") ) { 
  464.                         try {    
  465.                         anchor = new URL(null,  before + urlarr[i+1] + after);showStatus("Fetching ...");showDocument(anchor); 
  466.                         } catch (MalformedURLException e) {    
  467.                         anchor = null;  
  468.                         } 
  469.                         } else {
  470.                         try {    
  471.                         anchor = new URL(null,  destarr[i+1]);showStatus("Fetching ...");showDocument(anchor); 
  472.                         } catch (MalformedURLException e) {     
  473.                         anchor = null;   
  474.                         }  
  475.                       }
  476.                         break;
  477.                     }
  478.                 }
  479.         }
  480.  
  481.  
  482.         }
  483.           if (evt.id == Event.MOUSE_MOVE) {
  484.             in = true;
  485.             mousepos = evt.x;
  486.           }
  487.           if (evt.id == Event.MOUSE_EXIT) {
  488.             showStatus( " ") ;
  489.         in = false;
  490.           }
  491.           return mouse;
  492.         }
  493.  
  494. }
  495.